package com.lzz.shiro;

import com.lzz.dao.PermissionDao;
import com.lzz.entity.Permission;
import com.lzz.entity.Role;
import com.lzz.entity.User;
import com.lzz.service.PermissionService;
import com.lzz.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 自定义realm。完成具体的查数据或是其他方式的认证操作。做的是具体活
 * @author zhenzhen
 * @date 2018-12-20 下午8:22
 */
public class MyShiroRealm extends AuthorizingRealm {
    @Autowired
    private UserService userService;
    @Autowired
    private PermissionService permissionService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("===========进入授权代码块=============");
        // 获取用户名
        String username = (String) principalCollection.getPrimaryPrincipal();
        User user = userService.getUserByUserName(username);
        // 添加角色权限
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        List<Role> roleList = user.getRoleList();
        for (Role role : roleList) {
            simpleAuthorizationInfo.addRole(role.getRole());
            String roleId = role.getUuid();
            List<Permission> permissionList = permissionService.getPermissionByRoleId(roleId);
            for (Permission permission : permissionList) {
                simpleAuthorizationInfo.addStringPermission(permission.getPermission());
            }
        }
        return simpleAuthorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        // 加这一步的目的是在Post请求的时候先进性认证，然后到请求
        if(authenticationToken.getPrincipal() == null) {
            return null;
        }

        // 获取subject提交来的用户名
        // UsernameAndPassword是AuthenticationToken的一个子类
        String userName = authenticationToken.getPrincipal().toString();

        // 查询数据库，是否存在此用户
        User user = userService.getUserByUserName(userName);

        if (user == null) {
            // 这里返回会报出对应的异常
            return null;
        } else {
            // 数据库中该用户对应的密码
            String password = user.getPassword();
            // 数据中该用户用来加密使用的salt
            String salt = user.getSalt();

            // 开始验证, SimpleAuthenticationInfo封装了验证信息
            // 第三个参数是自定义realm的名字
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(userName,  user.getPassword(), getName());
            // 设置加密用的盐salt
            simpleAuthenticationInfo.setCredentialsSalt(ByteSource.Util.bytes(salt));
            return simpleAuthenticationInfo;
        }
    }
}
